local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.Server_PlayerComponent(),script.Parent.Name);

local AnalyticsService = game:GetService("AnalyticsService")
gOnboardingFunnel=require(script.Parent.OnboardingFunnel)

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
    self.PlayerInst=self.parent.main_inst

    self.DataBase=self.parent:GetComponent(ServerOHOH.load.Server_PlayerDataComponent())

    self.HadOnboardingFunnel={}


    self.OnboardingFunnelEvent=self.parent.OnboardingFunnelEvent

    self.OnboardingFunnelEvent:Connect(function(stepId,customFields)

        self:AddProcess(function()
            self:AddOnboardingFunnel(stepId,customFields)
        end)
    end)

    self.parent.LevelPassEvent:Connect(function(levelId)
        if levelId==1 then
            self.OnboardingFunnelEvent:Action("level_2")
        end
    end)

    self.OnboardingFunnelEvent:Action("enter")

end;

function M.dtor(self)
	M.super.dtor(self);
end;


function M.multi_step_name(self,setpName,customFields)
    local countKey=setpName.."_count"
    if not self[countKey] then
        self[countKey]=1
    else
       local nextId=  self[countKey]+1
       local id=string.format("%s_%s",setpName,nextId)
       local cfg= gOnboardingFunnel[id]
       if not cfg then
            return 
       end
       if cfg.preDone then
            if  self.HadOnboardingFunnel[cfg.preDone] then
                self[countKey]=nextId
            end
       else
            self[countKey]=nextId
       end
    end

    local id=string.format("%s_%s", setpName,self[countKey])


    if not gOnboardingFunnel[id] then
        return 
    end
    self:AddOnboardingFunnel(id,customFields)

end


function M.AddOnboardingFunnel(self,id,customFields)

    if gOnboardingFunnel.multi_step_name[id] then
        self:multi_step_name(id,customFields)
        return 
    end

    if not gOnboardingFunnel[id] then

        return 
    end

    if  self.HadOnboardingFunnel[id] then --只打点一次，已经打过的不用发送
        return
    end


    local step=gOnboardingFunnel[id].step
    local name=gOnboardingFunnel[id].name

    -- print("事件打点--->",id)
    AnalyticsService:LogOnboardingFunnelStepEvent(
        self.PlayerInst,
        step, -- Step number
        name, -- Step name
        customFields
    )

    self.HadOnboardingFunnel[id]=true
end


function M.AddLogEconomyEvent(self,key,add,now,AnalyticsEconomyFlowType,AnalyticsEconomyTransactionType)

    if not AnalyticsEconomyFlowType then
        AnalyticsEconomyFlowType= Enum.AnalyticsEconomyFlowType.Source
    end

    if not AnalyticsEconomyTransactionType then
        AnalyticsEconomyTransactionType=Enum.AnalyticsEconomyTransactionType.Gameplay.Name
    end

    AnalyticsService:LogEconomyEvent(
        self.PlayerInst,
        AnalyticsEconomyFlowType,
        key, -- Currency name
        add, -- Amount earned
        now, -- Current balance
        AnalyticsEconomyTransactionType -- Transaction type
    )
end

local EventList = ServerOHOH.load.EventList();

M:RegistEvent(EventList.game_analytics_onboardingFunnelEvent,M.AddOnboardingFunnel)

return M